<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDept('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 使用树形组件 -->
            <el-tree
              :data="treeData"
              default-expand-all
              class="departments-tree"
              :props="defaultProps"
            >
              <!-- @node-click="handleNodeClick" -->
              <!-- 从哪里解构赋值？？？？？？ -->
              <template slot-scope="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  style="height: 50px; width: 100%"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <p>{{ data }}</p>
                            <el-dropdown-item
                              @click.native="addDept(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDept(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && data.children.length === 0"

                              @click.native="delDept(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 对话框组件 -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <deptDialog
            v-if="showDialog"
            :origin-list="originList"
            :is-edit="isEdit"
            :parent-id="parentId"
            @update-dialog="updateDepart"
            @close="closeDialog"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入弹窗模块
import deptDialog from './deptDialog'
import { getDepartments, delDepartment } from '@/api/departments'
import { transTree } from '@/utils' // 引入转换树形结构的方法

export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制添加和编辑弹框隐藏和展示
      parentId: '', // 父级Id
      isEdit: '', // 用来判断点击的是编辑还是添加 添加为false，编辑为true
      originList: [] // 父传子判断是否部门名称编码重复
    }
  },
  created() {
    // 调用获取组织架构列表的方法
    this.getDepartments()
  },
  methods: {
    // 获取组织架构列表的方法
    async getDepartments() {
      const res = await getDepartments()
      // 把得到的数据格式化，为了之后判断部门名称编码是否已存在
      this.originList = res.data.depts.map((item) => {
        return {
          id: item.id,
          code: item.code,
          pid: item.pid,
          name: item.name
        }
      })
      console.log(res)
      this.treeData = transTree(res.data.depts)
    },

    // 添加部门
    addDept(id) {
      // console.log(id)
      this.parentId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 编辑部门
    editDept(id) {
      this.isEdit = true // 将编辑的状态传递给子组件
      this.showDialog = true
      this.parentId = id
    },

    // 关闭弹窗
    updateDepart() {
      this.showDialog = false
      // 在重新调用获取结构的接口，渲染最新的
      this.getDepartments()
    },
    // 删除部门
    async delDept(id) {
      const delRes = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      // 如果结果是cancel证明点击的是取消
      if (delRes === 'cancel') return this.$message('您取消了删除')
      // 调用删除接口
      await delDepartment(id)
      // 重新渲染页面
      this.getDepartments()
    },
    closeDialog() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
